<?php
require_once 'model/Customer.php';
require_once 'model/User.php';
require_once 'model/Address.php';
require_once 'vo/CustomerVO.php';

class CustomerService 
{
	private $customer;
	private $user;
	private $address;
	
	function __construct() 
	{
		$this->customer = Customer::getInstance();
		$this->user = User::getInstance();
		$this->address = Address::getInstance();
	}
	
	public function saveCustomer(CustomerVO $customer)
	{
		$user = $this->user->saveUser($customer->user);
		$customer->idUser = $user->idUser;
		$customer = $this->customer->saveCustomer($customer);
		$customer->user = $user;
		
		return $customer;
	}
	
	public function updateCustomer(CustomerVO $customer)
	{
		$user = $this->user->updateUser($customer->user);
		$customer = $this->customer->updateCustomer($customer);
		$customer->user = $user;
		
		return $customer;
	}

	public function listCustomer()
	{
		$users = $this->user->listUser(UserVO::CUSTOMER);
		
		if ($users != null)
			return $this->customer->listCustomer($users);
		else 
			return null;
	}
		
	public function deleteCustomer(CustomerVO $customer)
	{
		$this->user->deleteUser($customer->user->idUser);
		$this->address->deleteAddressByCustomer($customer->idCustomer);
		return $this->customer->deleteCustomer($customer->idCustomer);
	}
	
	public function loadCustomer(CustomerVO $customer)
	{
		$user = $this->user->loadUser($customer->idUser);
		$customer = $this->customer->loadCustomer($customer->idCustomer);
		$address = $this->address->listAddressByCustomer($customer->idCustomer);
		$customer->user = $user;
		$customer->address = $address;
		
		return $customer;
	}
	
	public function searchCustomer($name)
	{
		$users = $this->user->searchUser($name, UserVO::CUSTOMER);
		
		if ($users != null)
			return $this->customer->listCustomer($users);
		else 
			return null;
	}
	
}